package com.whfp.coky.ui.entry

import android.view.KeyEvent
import android.view.animation.AnimationUtils
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.whfp.coky.R
import com.whfp.coky.api.error.ErrorResult
import com.whfp.coky.databinding.ActivityLoginBinding
import com.whfp.coky.manager.InputTextManage
import com.whfp.coky.other.KeyboardWatcher
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.entry.vm.LoginVM
import com.whfp.coky.utils.LogUtil
import com.whfp.coky.widget.clicksDelay


class LoginAct : BaseActivity<LoginVM, ActivityLoginBinding>(), TextView.OnEditorActionListener{

    override fun isStatusBarDarkFont(): Boolean {
        return !super.isStatusBarDarkFont()
    }

    override fun initView() {
        vBinding.etLoginPassword.setOnEditorActionListener(this@LoginAct)
        vBinding.btnLoginCommit?.let {
            InputTextManage.with(this)
                .addView(vBinding.etLoginPhone)
                .addView(vBinding.etLoginPhone)
                .setMain(it)
                .build()
        }
        vBinding.btnLoginCommit.clicksDelay{
            if (vBinding.etLoginPhone?.text.toString().length != 11) {
                vBinding.etLoginPhone?.startAnimation(
                    AnimationUtils.loadAnimation(
                        getContext(),
                        R.anim.shake_anim
                    )
                )
                vBinding.btnLoginCommit?.showError(3000)
                toast(R.string.common_phone_input_error)
            }else{
                hideKeyboard(currentFocus)
                vModel.commit()
            }
        }


    }

    override fun initData() {
        postDelayed({
            KeyboardWatcher.with(this@LoginAct)
                .setListener(vModel)
        }, 500)
    }


    override fun errorResult(errorResult: ErrorResult) {
        super.errorResult(errorResult)
        vBinding.btnLoginCommit.showError(1000)
        toast(errorResult.toString())
    }

    override fun onEditorAction(p0: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // 模拟点击提交按钮
                LogUtil.e("click...")
            vBinding.btnLoginCommit?.let {
                if (it.isEnabled) {
                    // 模拟点击登录按钮
                    it.performClick()
                    return true
                }
            }
        }
        return false
    }
}